﻿using DoNet.Common.Pages;
using DoNet.Core;
using DoNet.Core.Dtos;
using DoNet.System.Dtos;
using DoNet.System.IRepositories;
using DoNet.System.IServices;
using DoNet.System.Models;
using DoNet.ThirdParty.AutoMapper;
using SqlSugar;

namespace DoNet.System.Services
{
    /// <summary>
    /// 应用数据
    /// 可以改成数据库操作
    /// </summary>
    public class AppService : BaseService<App, AppOutputDto>, IAppService
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly IAppRepository repository;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_baseRepository"></param>
        /// <param name="_repository"></param>
        public AppService(IRepository<App> _baseRepository, IAppRepository _repository)
        {
            baseRepository = _baseRepository;
            repository = _repository;
        }

        #region 同步方法
        /// <summary>
        /// 同步获取app对象
        /// </summary>
        /// <param name="appid">应用ID</param>
        /// <returns></returns>
        public App GetApp(string appid)
        {
            return repository.GetApp(appid);
        }
        /// <summary>
        /// 同步获取app对象
        /// </summary>
        /// <param name="appid">应用ID</param>
        /// <param name="secret">应用密钥AppSecret</param>
        /// <returns></returns>
        public App GetApp(string appid, string secret)
        {
            return repository.GetApp(appid, secret);
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="search">查询的条件</param>
        /// <returns>指定对象的集合</returns>
        public PageResult<AppOutputDto> FindWithPagerSearch(SearchInputDto<App> search)
        {
            bool order = search.Order == "asc" ? false : true;
            var exp = Expressionable.Create<App>()
               .AndIF(!string.IsNullOrEmpty(search.Keywords), x => x.AppId.Contains(search.Keywords) || x.RequestUrl.Contains(search.Keywords))
               .ToExpression();
            PagerInfo pagerInfo = new PagerInfo
            {
                CurrenetPageIndex = search.CurrenetPageIndex,
                PageSize = search.PageSize
            };
            List<App> list = baseRepository.FindWithPager(exp, pagerInfo, search.Sort, order);
            PageResult<AppOutputDto> pageResult = new PageResult<AppOutputDto>
            {
                CurrentPage = pagerInfo.CurrenetPageIndex,
                Items = list.MapTo<AppOutputDto>(),
                ItemsPerPage = pagerInfo.PageSize,
                TotalItems = pagerInfo.RecordCount
            };
            return pageResult;
        }
        #endregion 同步方法

        #region 异步方法
        /// <summary>
        /// 获取app对象
        /// </summary>
        /// <param name="appid">应用ID</param>
        /// <returns></returns>
        public async Task<App> GetAppAsync(string appid)
        {
            return await repository.GetAppAsync(appid);
        }
        /// <summary>
        /// 获取app对象
        /// </summary>
        /// <param name="appid">应用ID</param>
        /// <param name="secret">应用密钥AppSecret</param>
        /// <returns></returns>
        public async Task<App> GetAppAsync(string appid, string secret)
        {
            return await repository.GetAppAsync(appid, secret);
        }
        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="search">查询的条件</param>
        /// <returns>指定对象的集合</returns>
        public async Task<PageResult<AppOutputDto>> FindWithPagerSearchAsync(SearchInputDto<App> search)
        {
            bool order = search.Order == "asc" ? false : true;
            var exp = Expressionable.Create<App>()
               .AndIF(!string.IsNullOrEmpty(search.Keywords), x => x.AppId.Contains(search.Keywords) || x.RequestUrl.Contains(search.Keywords))
               .ToExpression();
            PagerInfo pagerInfo = new PagerInfo
            {
                CurrenetPageIndex = search.CurrenetPageIndex,
                PageSize = search.PageSize
            };
            List<App> list = await baseRepository.FindWithPagerAsync(exp, pagerInfo, search.Sort, order);
            PageResult<AppOutputDto> pageResult = new PageResult<AppOutputDto>
            {
                CurrentPage = pagerInfo.CurrenetPageIndex,
                Items = list.MapTo<AppOutputDto>(),
                ItemsPerPage = pagerInfo.PageSize,
                TotalItems = pagerInfo.RecordCount
            };
            return pageResult;
        }
        #endregion 异步方法
    }
}
